import { createRouter, createWebHistory } from 'vue-router'

import LoginItem from '@/views/login/LoginItem.vue'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path:'/',
      name:'login',
      component:LoginItem
    },
    {
      path: '/map',
      name: 'map',
      component: () => import('../views/mapView.vue')
    },
  ]
})
router.beforeEach((to, from, next) => {
  var auth = localStorage.getItem('token');
  // 当用户成功登录后，无法再直接返回登录页面，需要在指定登出界面完成退出后才能返回登录界面
  if (to.path == "/" ) {
    if (auth) {
      router.push("/map")
    } else {
      next()
    }
  } else {
    if (auth) {
      next();
    } else {
      router.push("/")
      alert("请先登录")
    }
  }
})
export default router
